<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-02-23
 * Time: 11:09
 */
namespace app\api\common;

use think\Controller;
use think\Db;
use think\Exception;

class Feedback extends Controller
{
    protected static $err_code = 0;

    /**
     * @apiGroup Feedback
     * @apiVersion 1.0.1
     * @apiDescription 反馈
     * @api {post} /feedback.index 反馈
     * @apiName Feedback_index
     * @apiParam {int} member_id 用户id
     * @apiParam {String} contact 联系方式
     * @apiParam {String} content 内容
     *
     * @apiSuccess {int} code 结果码
     * @apiSuccess {String} msg 消息说明
     * @apiSuccessExample 成功示例
     *  HTTP/1.1 200 OK
     *  {
     *     code:200,
     *     msg:'success',
     *  }
     *
     *  @apiError code 0 系统异常错误
     *  @apiError msg token已经过期
     *  @apiErrorExample {json} 失败示例
     *  HTTP/1.1 404 Not Found
     *  {
     *     code:0,
     *      msg:'失败或没有数据返回',
     *   }
     */

    public static function index($param){
        //启动事物
        Db::startTrans();
        try{
            if (Db::table('yf_feedback')->insert(['content'=>trim($param['content']),'member_id'=>trim($param['member_id']),'contact'=>trim($param['contact']),'add_time'=>time()])){
                // 事务提交
                Db::commit();
                return ['code' => 200, 'msg' => 'SUCCESS'];
            }else{
                self::$err_code = 400023;
                throw new Exception(config('error_code.'.self::$err_code));
            }
        }catch (Exception $e){
            // 回滚事务
            Db::rollback();
            return ['code' => self::$err_code, 'msg' => $e->getMessage()];
        }
    }
}